package study.spring.core.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
// 下面这两种注解都可以
// @Aspect("perthis(this(study.spring.core.aop.TargetObjectPrototype))")
@Aspect("pertarget(target(study.spring.core.aop.TargetObjectPrototype))")
@Scope("prototype")
public class AspectNotSingleton {
  protected final Log log = LogFactory.getLog(getClass());

  @Before(value = "execution(public * study.spring.core.aop.TargetObjectPrototype.prototypeAspectUse(..))")
  // @Before(value = "within(study.spring.core.aop.TargetObjectPrototype)")
  public void recordServiceUsage() {
    log.info("这是一个 prototype aspect bean在执行前置通知, 它的hashcode: " + this.hashCode());
  }

  // @After/@Before 中可以直接写表达式, 也可以指向一个 @Pointcut 修饰的方法
  @After("pointCut()")
  public void recordServiceUsage2() {
    log.info("这是一个 prototype aspect bean在执行后置通知, 它的hashcode: " + this.hashCode());
  }

  @Pointcut(value = "execution(public * study.spring.core.aop.TargetObjectPrototype.prototypeAspectUse(..))")
  public void pointCut() {
  }

}
